package org.reenscape.packet;
import org.reenscape.model.*;
import org.reenscape.updater.*;
import org.reenscape.lib.*;
public class PacketPlayer {

		/**
		* Sending a trade request to another player.
		*/
		public void TradeRequest(PacketParser p3){
			p3.WanneTradeWith = p3.inStream.readUnsignedWord();
			p3.WanneTrade = 1;
		}
		
		
		/**
		* Clicking attack on another Player.
		*/
		public void Attack(PacketParser p3){
			int Slot = p3.inStream.readSignedWordBigEndian();
			p3.MeleePlayerSlot = Slot;
		}
		
		/**
		* Update chat settings.
		*/
		public void UpdateSettings(PacketParser p3){
			int tradeBlock = p3.inStream.readUnsignedByte();
			int privateBlock = p3.inStream.readUnsignedByte();
			int publicBlock = p3.inStream.readUnsignedByte();
			p3.Privatechat = privateBlock;
			
			for(int i1 = 1; i1 < ClientUpdater.maxPlayers; i1++) {
				if(ClientUpdater.players[i1] != null && ClientUpdater.players[i1].isActive == true) {
					ClientUpdater.players[i1].pmupdate(p3.playerId, p3.GetWorld(p3.playerId));
				}
			}
		}
	
		/**
		* Answering the request of a trade.
		*/
		public void TradeAnswer(PacketParser p3){
			p3.WanneTradeWith = p3.inStream.readSignedWordBigEndian();
			p3.WanneTrade = 2;
		}
		
		/**
		* Focus change - whether your looking at the screen or not.
		*/
		public void Focus(PacketParser p3){
			int Focus = p3.inStream.readUnsignedWordBigEndian();
			boolean HasFocus = (Focus != 0);
		}
		
		/**
		* Following another player.
		*/
		public void Follow(PacketParser p3){
			int ID = p3.inStream.readSignedWordBigEndian();
			if(ID > ClientUpdater.maxPlayers) return; 
			p3.FollowingID = ID;
			Follow.UpdateFollow(p3, (PacketParser) ClientUpdater.players[ID]);
		}
		
		/**
		* Commands sent by administrators; normal or moderator level commands are ignored.
		*/
		public void Command(PacketParser p3){
			String command = p3.inStream.readString();
			if(p3.playerRights >= 2 && p3.playerRights <= 3){
				p3.customCommand(command);
			}
		}
		
		/**
		* Adds a friend to the user's friendslist.
		*/
		public void AddFriend(PacketParser p3){
			long ID = p3.inStream.readQWord();
			boolean Valid = true;
			for(int i1 = 0; i1 < p3.friends.length; i1++) {
				if(p3.friends[i1] != 0 && p3.friends[i1] == ID) {
					Valid = false;
					p3.sendMessage(ID+" is already in your friendlist.");
				}
			}
			
			if(Valid == true) {
				for(int i1 = 0; i1 < p3.friends.length; i1++) {
					if(p3.friends[i1] == 0) {
						p3.friends[i1] = ID;
						for(int i2 = 1; i2 < ClientUpdater.maxPlayers; i2++) {
							if(ClientUpdater.players[i2] != null && ClientUpdater.players[i2].isActive && Library.mutil.playerNameToInt64(ClientUpdater.players[i2].playerName) == ID) {
								if(p3.playerRights >= 2 || ClientUpdater.players[i2].Privatechat == 0 ||(ClientUpdater.players[i2].Privatechat == 1 && ClientUpdater.players[i2].isinpm(Library.mutil.playerNameToInt64(p3.playerName)))) {
									p3.loadpm(ID, p3.GetWorld(i2));
									break;
								}
							}
						}
						break;
					}
				}
			}
		}
		
		/**
		* Removes a friend from the player's friendslist.
		*/
		public void RemoveFriend(PacketParser p3){
			long ID = p3.inStream.readQWord();
			for(int i1 = 0; i1 < p3.friends.length; i1++) {
				if(p3.friends[i1] == ID) {
					p3.friends[i1] = 0;
					break;
				}
			}
		}
		
		/**
		* Add ignore
		*/
		public void AddIgnore(PacketParser p3){
			long ID = p3.inStream.readQWord();
			for(int i10 = 0; i10 < p3.ignores.length; i10++) {
				if(p3.ignores[i10] == 0) {
					p3.ignores[i10] = ID;
					break;
				}
			}
		}
		
		/**
		* Remove ignore
		*/
		public void RemoveIgnore(PacketParser p3){
			long ID = p3.inStream.readQWord();
			for(int i11 = 0; i11 < p3.ignores.length; i11++) {
				if(p3.ignores[i11] == ID) {
					p3.ignores[i11] = 0;
					break;
				}
			}
		}
		
		/**
		* Sends a private message.
		*/
		public void PM(PacketParser p3){
			long ID = p3.inStream.readQWord();
			byte text[] = new byte[100];
			int size =(byte)(p3.packetSize-8);
			p3.inStream.readBytes(text, size, 0);
			for(int i1 = 0; i1 < p3.friends.length; i1++) {
				if(p3.friends[i1] == ID) {
					boolean pmsent = false;
					for(int i2 = 1; i2 < ClientUpdater.maxPlayers; i2++) {
						if(ClientUpdater.players[i2] != null && ClientUpdater.players[i2].isActive && Library.mutil.playerNameToInt64(ClientUpdater.players[i2].playerName) == ID) {
							if(p3.playerRights >= 2 || ClientUpdater.players[i2].Privatechat == 0 || (ClientUpdater.players[i2].Privatechat == 1 && ClientUpdater.players[i2].isinpm(Library.mutil.playerNameToInt64(p3.playerName)))) {
								ClientUpdater.players[i2].sendpm(Library.mutil.playerNameToInt64(p3.playerName), p3.playerRights, text, size);
								pmsent = true;
							}
							break;
						}
					}
					if(!pmsent) {
						p3.sendMessage("The message failed to send because that user is currently offline.");
						break;
					}
				}
			}
		}
		
		public void PlayerDialogue(String s, PacketParser p3){ // One line
			p3.sendFrame200(615, 974);
			p3.sendFrame126(p3.playerName, 970);
			p3.sendFrame126(s, 971);
			p3.sendFrame185(969);
			p3.sendFrame164(968);
			p3.NpcDialogueSend = true;
		}
		
		public void PlayerDialogue(String s, String s2, PacketParser p3){ // Two lines
			p3.sendFrame200(615, 974);
			p3.sendFrame126(p3.playerName, 975);
			p3.sendFrame126(s, 976);
		    p3.sendFrame126(s2, 977);
		    p3.sendFrame185(974);
			p3.sendFrame164(973);
			p3.NpcDialogueSend = true;
		}
		
		public void PlayerDialogue(String s, String s2, String s3, PacketParser p3){ // Three lines
		    p3.sendFrame200(615, 974);
		    p3.sendFrame126(p3.playerName, 981);
		    p3.sendFrame126(s, 982);
		    p3.sendFrame126(s2, 983);
		    p3.sendFrame126(s3, 984);
		    p3.sendFrame185(980);
		    p3.sendFrame164(979);
		    p3.NpcDialogueSend = true;
		}

		public void PlayerDialogue(String s, String s2, String s3, String s4, PacketParser p3){ // Four lines
		    p3.sendFrame200(615, 974);
		    p3.sendFrame126(p3.playerName, 988);
		    p3.sendFrame126(s, 989);
		    p3.sendFrame126(s2, 990);
		    p3.sendFrame126(s3, 991);
		    p3.sendFrame126(s4, 992);
		    p3.sendFrame185(987);
		    p3.sendFrame164(986);
		    p3.NpcDialogueSend = true;
		}
}